import {
	defineStore
} from 'pinia';
import {
	SERVER_URL
} from "@/config/index.js"

export const useUserStore = defineStore('user', {
	state: () => {
		return {
			id: undefined,
			created_on: undefined,
			openid: undefined,
			level: undefined,
			nickname: undefined,
			avatar: undefined,
			phone: undefined,
			gender: undefined,
			last_login_time: undefined,
			expires: undefined,
			token: undefined,
			like_article_comments: [],
			like_articles: []
		};
	},
	actions: {
		login(callback) {
			uni.showLoading({
				title: "登陆中"
			})

			const showFailToast = () => {
				uni.hideLoading()
				uni.showToast({
					icon: "error",
					title: "登录失败"
				})
			}

			uni.login({
				provider: "weixin",
				success: (e) => {
					console.log(e)
					uni.request({
						url: SERVER_URL + '/customer/login',
						method: "POST",
						data: {
							code: e.code
						},
						header: {
							"content-type": "application/json"
						},
						success: (res) => {
							console.log(res);
							if (res.statusCode === 200) {
								this.$patch(res.data)
								if (callback) callback()
								uni.hideLoading()
								uni.showToast({
									icon: "success",
									title: "登录成功"
								})
							} else {
								showFailToast()
							}
						},
						fail: () => {
							showFailToast()
						}
					});
				},
				fail: () => {
					showFailToast()
				}
			})
		},
		logout() {
			this.$patch({
				id: undefined,
				created_on: undefined,
				openid: undefined,
				level: undefined,
				nickname: undefined,
				avatar: undefined,
				phone: undefined,
				gender: undefined,
				last_login_time: undefined,
				expires: undefined,
				token: undefined,
				like_article_comments: [],
				like_articles: []
			})
		},
		refreshToken() {
			console.log(this.token)
			if (this.token) {
				uni.request({
					url: SERVER_URL + '/customer/refresh-token',
					method: "POST",
					data: {},
					header: {
						"content-type": "application/json",
						"authorization": this.token
					},
					success: (res) => {
						console.log(res);
						if (res.statusCode === 200) {
							this.$patch(res.data)
						} else {
							this.logout()
						}
					},
					fail: () => {
						this.logout()
					}
				});
			}
		},
		loadArticleLike() {
			return new Promise((resolve, reject) => {
				uni.request({
					url: SERVER_URL + '/customer/like/article',
					method: "GET",
					header: {
						"authorization": this.token
					},
					success: (res) => {
						if (res.statusCode === 200) {
							this.like_articles = res.data
							resolve()
						} else {
							reject()
						}
					},
					fail: (e) => {
						reject(e)
					}
				});
			})
		},
		loadArticleCommentLike() {
			return new Promise((resolve, reject) => {
				uni.request({
					url: SERVER_URL + '/customer/like/article-comment',
					method: "GET",
					header: {
						"authorization": this.token
					},
					success: (res) => {
						if (res.statusCode === 200) {
							this.like_article_comments = res.data
							resolve()
						} else {
							reject()
						}
					},
					fail: (e) => {
						reject(e)
					}
				});
			})
		}
	},
	persist: {
		// 调整为兼容多端的API
		storage: {
			setItem(key, value) {
				uni.setStorageSync(key, value)
			},
			getItem(key) {
				return uni.getStorageSync(key)
			},
		},
	}
});